package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.system.domain.WdyDirectReward;
import com.ruoyi.system.domain.WdyUser;
import com.ruoyi.system.service.IWdyDirectRewardService;
import com.ruoyi.system.service.IWdyUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 直推奖 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-12-26
 */
@Controller
@RequestMapping("/system/wdyDirectReward")
public class WdyDirectRewardController extends BaseController
{
    private String prefix = "system/wdyDirectReward";
	
	@Autowired
	private IWdyDirectRewardService wdyDirectRewardService;

	@Autowired
	private IWdyUserService wdyUserService;

	@RequiresPermissions("system:wdyDirectReward:view")
	@GetMapping()
	public String wdyDirectReward()
	{
	    return prefix + "/wdyDirectReward";
	}
	
	/**
	 * 查询直推奖列表
	 */
	@RequiresPermissions("system:wdyDirectReward:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(WdyDirectReward wdyDirectReward)
	{
		startPage();
        List<WdyDirectReward> list = wdyDirectRewardService.selectWdyDirectRewardList(wdyDirectReward);
		for(WdyDirectReward e : list) {
			if(StringUtils.isNotEmpty(e.getCreateBy())) {
				QueryWrapper<WdyUser> wdyUserQueryWrapper = new QueryWrapper<>();
				wdyUserQueryWrapper.eq("user_id", e.getCreateBy());
				WdyUser user = wdyUserService.getOne(wdyUserQueryWrapper);
				e.addExtraData("userName", user.getUserName());
			}
		}
		return getDataTable(list);
	}
	
	
	/**
	 * 导出直推奖列表
	 */
	@RequiresPermissions("system:wdyDirectReward:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WdyDirectReward wdyDirectReward)
    {
    	List<WdyDirectReward> list = wdyDirectRewardService.selectWdyDirectRewardList(wdyDirectReward);
        ExcelUtil<WdyDirectReward> util = new ExcelUtil<WdyDirectReward>(WdyDirectReward.class);
        return util.exportExcel(list, "wdyDirectReward");
    }
	
	/**
	 * 新增直推奖
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存直推奖
	 */
	@RequiresPermissions("system:wdyDirectReward:add")
	@Log(title = "直推奖", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(WdyDirectReward wdyDirectReward)
	{		
		return toAjax(wdyDirectRewardService.insertWdyDirectReward(wdyDirectReward));
	}

	/**
	 * 修改直推奖
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		WdyDirectReward wdyDirectReward = wdyDirectRewardService.selectWdyDirectRewardById(id);
		mmap.put("wdyDirectReward", wdyDirectReward);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存直推奖
	 */
	@RequiresPermissions("system:wdyDirectReward:edit")
	@Log(title = "直推奖", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(WdyDirectReward wdyDirectReward)
	{		
		return toAjax(wdyDirectRewardService.updateWdyDirectReward(wdyDirectReward));
	}
	
	/**
	 * 删除直推奖
	 */
	@RequiresPermissions("system:wdyDirectReward:remove")
	@Log(title = "直推奖", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(wdyDirectRewardService.deleteWdyDirectRewardByIds(ids));
	}
	
}
